*WIC co-op eWIC new diff in diff analysis
*last modified: 12 December 2024
*last modified by: Charlotte Ambrozek
*this do file implements the csdid2 package to estimate individual 2x2 doubly robust did estimates and aggregate to obtain an att 

clear all
set more off
set rmsg on

set maxvar 120000
set emptycells drop

local data_dir ./data/cleaned
local raw_dir ./data/raw
local out_dir ./analysis/output
local graph_dir ./analysis/output/graphs
local tab_dir ./analysis/output/tables
local log_dir ./documentation/logs
local date: display %tdYY-NN-DD date(c(current_date), "DMY")
di "`date'"
capture log close

log using `log_dir'/ewic_csdid_tip_chainprocessortime_`date', replace

*import cleaned eWIC implementation info into own frame
*eWIC implementation variable is at county fips/fiscal year level 
*partition data into state/treatment year groups

*import cleaned ewic implementation info into own frame
mkf ewic_fy
frame ewic_fy{
	use ./data/cleaned/ewic_rollout.dta, clear
	rename year fiscalyear
	drop pre* post* 
}

mkf income
frame income{
	use ./data/cleaned/countyyearmedianincome.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf countychars
frame countychars{
	use ./data/cleaned/countystats.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf demos
frame demos{
	use ./data/cleaned/demos.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

*import TIP store data into own frame
mkf tip_fy
frame tip_fy{
	local data_dir ./data/cleaned
	use `data_dir'/tip_auth_sq, clear
	*drop "Direct Distribution Center" and "Home Food Delivery Contractor" types
	gen f = (vendor_type1 == 3 & (vendor_type2 == 4 | missing(vendor_type2))) | (vendor_type1 == 4 & (vendor_type2 == 3 | missing(vendor_type2)))
	bys tip_id: egen flag = mode(f)
	bys tip_id: replace vendor_type1 = vendor_type1[_n+1] if flag == 0 & f == 1
	drop if flag == 1
	drop flag f tip_year_id
	*fill in gaps in state fips using modes
	generate str_fips = string(ct_fips)
	replace str_fips = "0" + str_fips if strlen(str_fips) == 4
	generate st_fips = substr(str_fips, 1, 2)
	destring st_fips, replace 
	
	*drop Vermont and mississippi because of different regiemes prior to eWIC
	*drop NV because ewic turn on then off then on again and we don't have reliable timings
	*drop missing st_fips
	drop if st_fips == 50 | st_fips == 32 | st_fips == 28 | missing(st_fips)

	*flag stores that specialize in WIC
	gen a50 = vendor_type1 == 1 | vendor_type1 == 7 | vendor_type2 == 1 | vendor_type2 == 7
	
	*link in low income/high poverty info
	frlink m:1 ct_fips, frame(income) generate(inc_link)
	frget lowinc highpov highwicelig, from(inc_link)

	*link ewic implementation info
	frlink m:1 ct_fips fiscalyear, frame(ewic_fy) generate(ewic_link)
	frget ev_year , from(ewic_link)
	* assume treated at earliest exposure
	bys tip_id: egen ey_min = min(ev_year)
	
	* assume treated at earliest exposure
	replace ev_year = ey_min if ev_year != ey_min & !missing(ey_min)
	bys tip_id (ev_year): assert ev_year[1] == ev_year[_N]

	*assert no dups at future level of xtset 
	duplicates report tip_id fiscalyear 
	assert r(N) == r(unique_value)
	xtset tip_id fiscalyear
	compress
}

cwf tip_fy
* for chains, construct two alternative treatment timings:
* 1) based on the first implementation of eWIC in the state for each chain
* 2) based on the first implementation of eWIC by processor for each chain
frame put ev_year vendor_name state st_fips if chain == 1, into(chain_timings)

cwf chain_timings
duplicates drop

* clean up chain names because these will be needed to define the timings
gen name = vendor_name
*first, make seven eleven text
replace name = regexr(name, "^7\s11", "SEVEN ELEVEN")
* find position of first numeric character after at least one alpha character, and drop rest of string
* this should drop all store numbers, etc
*first, find position of first alpha character
moss name, match("([A-Za-z])") regex max(1)
rename _pos1 alphapos
drop _count _match1
moss name, match("([0-9])") regex
drop _match*
gen droppos = .
forval i = 9(-1)1{
	replace droppos = _pos`i' if _pos`i' > alphapos & _count > 1 & _pos`i' >= 4
}
replace name = substr(name, 1, droppos - 1)
replace name = strtrim(name)
replace name = regexr(name, "[0-9]$", "") if !missing(_pos1) & _pos1 > 2
replace name = strtrim(name)
drop _count _pos* alphapos droppos

* generate processor variable
gen process = ""
replace process = "conduent" if inlist(state, "VA", "OK", "CT", "NH", "NY")
replace process = "conduent" if inlist(state, "RI", "AL", "GA", "MS", "SC", "TN", "IN")
replace process = "solutran" if inlist(state, "NM", "TX", "ME", "MD", "NJ", "PA")
replace process = "solutran" if inlist(state, "NC", "IL", "AR", "LA", "UT", "MT")
*note that solutran implemented wyoming in 2002 before new mexico or texas
replace process = "cdp" if inlist(state, "MA", "WV", "FL", "KY", "WI", "OR", "DE", "DC")
replace process = "cdp" if inlist(state, "IA", "MI", "MN", "AZ", "CO", "KS", "MO", "NE")
*ohio seems to be hosted by their own state agency so don't fit in to the processor matrix
encode process, generate(processor)

bys name processor: egen processor_min_ev_year = min(ev_year)
replace processor_min_ev_year = . if missing(processor)

drop name process processor

cwf tip_fy
frlink m:1 vendor_name ev_year state st_fips, frame(chain_timings) generate(chain_time_link)
frget processor_min_ev_year, from(chain_time_link)
replace processor_min_ev_year = ev_year if missing(processor_min_ev_year)
drop chain_time_link

drop if missing(processor_min_ev_year)
frame drop ewic_fy

encode state, generate(state_code)

frlink m:1 ct_fips, frame(countychars) generate(countylink)
frget medincome sharepoor shareltwic sharelt2fpl sharesnap sharecash sharecashorsnap, from(countylink)
frlink m:1 ct_fips, frame(demos) generate(demoslink)
frget total_pop under5_pop hispanic_total_pop total_black_pop region division urbanrural share_black share_hispanic share_under5, from(demoslink)
gen log_pop = log(total_pop)
reg processor_min_ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.region i.urbanrural
outreg2 using ./analysis/output/tables/chainprocessortimingregression.tex, replace
reg processor_min_ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.urbanrural i.state_code
outreg2 using ./analysis/output/tables/chainprocessortimingregressionwinstate.tex, replace

mkf csdidsimple float(b se pval ll ul) str25(outcome subsample state controls)
mkf csdidevent float(b se pval ll ul) int(ev_yr) str25(outcome subsample state controls)
mkf csdideventavg float(avgb avgse avgpval avgll avgul) int(times) str25(time outcome subsample state controls)
mkf csdidgroup float(b se pval ll ul) str25(gr_yr outcome subsample state controls)

cd `out_dir'
csdid auth , ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_auth) replace cluster(st_fips)
mkf post
cwf post
use csdid_tip_proctime_auth, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("no")
}
* same estimation but with covariates for local (county level) economic conditions now
cwf tip_fy
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_auth_ctrls) replace cluster(st_fips)
cwf post 
use csdid_tip_proctime_auth_ctrls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("yes")
}

csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf tip_fy
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 28, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_auth_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_proctime_auth_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_auth_xs_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_proctime_auth_xs_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("yes")
	frame drop state
}

cwf tip_fy
frame put _all if chain == 1, into(chain)
cwf chain

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_authchain) replace cluster(st_fips)
cwf post
use csdid_tip_proctime_authchain, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("no")
}
* chain only analysis but with controls this time
cwf chain
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthchainxs) replace cluster(st_fips)
cwf post
use csdidtipproctimeauthchainxs, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf chain
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 28, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthchain`state') replace cluster(st_fips)
	cwf post
	use csdidtipproctimeauthchain`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("no")
	cwf chain
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthchainxs`state') replace cluster(st_fips)
	cwf post
	use csdidtipproctimeauthchainxs`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("yes")
	frame drop state
}

cwf tip_fy
frame put _all if chain == 0, into(indep)
cwf indep
frame drop chain
* indepedent store estimation
cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_authindep) replace cluster(st_fips)
cwf post
use csdid_tip_proctime_authindep, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("no")
}
*independent store estimation with covariates
cwf indep
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthindepxs) replace cluster(st_fips)
cwf post
use csdidtipproctimeauthindepxs, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf indep
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthindep`state') replace cluster(st_fips)
	cwf post
	use csdidtipproctimeauthindep`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("no")
	cwf indep
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdidtipproctimeauthindepxs`state') replace cluster(st_fips)
	cwf post
	use csdidtipproctimeauthindepxs`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("yes")
	frame drop state
}

/* cwf tip_fy
frame put _all if highwicelig == 1, into(highwicelig)
cwf highwicelig

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(processor_min_ev_year) notyet long2 saverif(csdid_tip_proctime_authhighwicelig) replace cluster(st_fips)
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("highwicelig") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("highwicelig") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("highwicelig") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("highwicelig") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("highwicelig") ("all") ("no")
} */

frame csdideventavg{
	expand times, generate(new)
	bys time outcome subsample controls (new): gen ev_yr = sum(new)
	replace ev_yr = ev_yr - 4 if time == "pre"
	drop time new times
}

cwf csdidevent
frlink 1:1 ev_yr outcome subsample controls, frame(csdideventavg)
frget avgb avgse avgpval avgll avgul, from(csdideventavg)
drop csdideventavg

frame drop csdideventavg

cwf csdidsimple
/* frame drop highwiceligindep */
save `out_dir'/csdid_tip_chainprocessortime_simple.dta, replace
cwf csdidevent
save `out_dir'/csdid_tip_chainprocessortime_event.dta, replace  
cwf csdidgroup
save `out_dir'/csdid_tip_chainprocessortime_group.dta, replace  
log close 

/** manual replacements where the process didn't work exactly because of name typos
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "BLODGETT")
replace processor_min_ev_year = 2008 if process == "conduent" & (regexm(name, "FOODTOWN") | regexm(name, "FOOD TOWN"))
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "FRESH CHOICE")
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "KROGERS")
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "LOUIES FRESH MARKET")
replace processor_min_ev_year = 2014 if process == "conduent" & regexm(name, "PIGGLY WIGGLY")
// replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "SAVE A LOT")
// replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "SHOP N SAVE")
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "UNITED SUPERMARKET")
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "WALGREEN")
replace processor_min_ev_year = 2008 if process == "conduent" & regexm(name, "WALMART")
replace processor_min_ev_year = 2009 if process == "cdp" & regexm(name, "CVS")
replace processor_min_ev_year = 2011 if process == "cdp" & regexm(name, "FOOD GIANT")
replace processor_min_ev_year = 2011 if process == "fcdp" & regexm(name, "HILLBILLY MARKET")
replace processor_min_ev_year = 2009 if process == "cdp" & regexm(name, "KROGER")
replace processor_min_ev_year = 2011 if process == "cdp" & regexm(name, "PIGGLY WIGGLY")
replace processor_min_ev_year = 2013 if process == "cdp" & regexm(name, "PUBLIX")
replace processor_min_ev_year = 2009 if process == "cdp" & regexm(name, "SAVE A")
replace processor_min_ev_year = 2011 if process == "cdp" & regexm(name, "STOP & SHOP")
replace processor_min_ev_year = 2013 if process == "cdp" & regexm(name, "TARGET")
replace processor_min_ev_year = 2009 if process == "cdp" & (regexm(name, "WAL MART") | regexm(name , "WALMART"))
replace processor_min_ev_year = 2011 if process == "cdp" & regexm(name, "WALGREESN")
replace processor_min_ev_year = 2004 if process == "solutran" & regexm(name, "ALBERTSONS")
replace processor_min_ev_year = 2005 if process == "solutran" & regexm(name, "BROOKSHIRE BROS")
replace processor_min_ev_year = 2004 if process == "solutran" & regexm(name, "COXS FOODARAMA")
replace processor_min_ev_year = 2008 if process == "solutran" & regexm(name, "GROCERY SVC")
replace processor_min_ev_year = 2005 if process == "solutran" & regexm(name, "MINYARD")
replace processor_min_ev_year = 2006 if process == "solutran" & regexm(name, "SUPER S")
replace processor_min_ev_year = 2004 if process == "solutran" & regexm(name, "TARGET")
replace processor_min_ev_year = 2004 if process == "solutran" & regexm(name, "WALMART")
replace processor_min_ev_year = 2005 if process == "solutran" & regexm(name, "WINCO")